package com.jc.oms.controller.yzp;

import com.jc.common.util.DateTimeUtil;
import com.jc.framework.util.PropertyPlaceholder;
import com.jc.oms.controller.BaseController;
import com.jc.service.systemloglist.SystemLogListService;
import com.jc.service.yzpService.YZPProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("all")
@RestController
@RequestMapping("yzpProduct")
public class YZPProductController extends BaseController {

    @Autowired
    private YZPProductService yzpProductService;

    Object appSecret = PropertyPlaceholder.getProperty("yzp.appSecret");
    @Autowired
    private SystemLogListService systemLogListService;

    //    @Scheduled(cron = "0 0/58 * * * * ") //设定为每1小时执行一次
    @GetMapping("/yzpProductList")
    public Map<String, Object> queryProductList() {
        Map<String, Object> resultMap = null;
        Map<String, Object> param = new HashMap<>();

//         System.out.println(appSecret);

            param.put("pageNo", 1);
            param.put("appSecret", appSecret);
            param.put("pageSize", 10000000);
            getLoger().debug(DateTimeUtil.getDate(DateTimeUtil.LONG_TIME_FORMAT), "请求优质品的页码为" + param.get("pageNo"));
            resultMap = this.yzpProductService.queryYzpProductList(param);


//        if (result.get("data") == null) {
//            resultMap.put("code", "800");
//            resultMap.put("status", "fail");
//            resultMap.put("msg", "没有数据");
//            return resultMap;
//        }
//
//        //System.out.println(param.get("pageNo"));
//        resultMap.put("code", "200");
//        resultMap.put("status", "success");
//        resultMap.put("data", result);

        return resultMap;
    }


   /* @GetMapping("updateTask")
    public void update() {
        Map<String, Object> param = new HashMap<>();


        param.put("pageNo", 1);
        param.put("appSecret", "c97120ac-3f2d-48e2-b8a7-65acf639d342");
        param.put("pageSize", 10000);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {

                try {
                    systemLogListService.addSystemLog(JSONObject.toJSONString(param), Conclusion.OK, null, LogType.PRODUCT_LIST_SELECT, "定时同步库存价格成功!");
//                    HttpUtil.get("http://120.27.224.207:8080/oms/yzpProduct/updateProducts");
                    HttpUtil.get("http://localhost:8080/oms/yzpProduct/updateProducts");
                } catch (Exception e) {
                    systemLogListService.addSystemLog(JSONObject.toJSONString(param), Conclusion.FAIL, ExceptionUtil.subExceptionMessage(e, 0, 150), LogType.PRODUCT_LIST_SELECT, "定时同步库存价格失败！");

                    e.printStackTrace();
                }
            }
        }, 1000, 3600000);

    }*/

    @GetMapping("updateProducts")
    private Map<String, Object> updateProducts() {

        Map<String, Object> params = new HashMap<>();

        params.put("pageNo", 1);
        params.put("appSecret", appSecret);
        params.put("pageSize", 10000);

        Map<String, Object> resultMap = new HashMap<String, Object>();

        Boolean flag = this.yzpProductService.updateProducts(params);
        if (flag) {
            resultMap.put("code", "200");
            resultMap.put("msg", "更新分销商品的库存的价格成功！~");
        } else {
            resultMap.put("code", "999");
            resultMap.put("msg", "更新库存和价格失败");
        }

        return resultMap;

    }
}
